<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>抽象工厂模式</title>
</head>
<body>
    <script>
        //抽象工厂
var VehicleFactory = function(subType, superType){//子类名称，父类名称
    if(typeof VehicleFactory[superType] === 'function'){//判断工厂中是否有该抽象类
        function F(){};//缓存类
        F.prototype = new VehicleFactory[superType]();//继承父类的属性和方法
        subType.constructor = subType;
        subType.prototype = new F();


    }else{
        throw new Error('未创建该抽象类');//若抽象类不存在则抛出异常
    }
}

//小汽车抽象类
VehicleFactory.Car = function(){
    this.type = 'car'
};
VehicleFactory.Car.prototype={
    getPrice:function(){
        return new Error('抽象方法不能调用');
    },
    getSpeed:function(){
        return new Error('抽象方法不能调用');
    }
}

//宝马汽车子类
var BMW=function(price, speed){
    this.price=price
    this.speed=speed
}
VehicleFactory(BMW,'Car');
BMW.prototype.getPrice=function(){
    return this.price
}
BMW.prototype.getSpeed=function(){
    return this.speed
}

var car = new BMW(20000,70);
console.log(car.getPrice());
console.log(car.getSpeed());



    </script>
</body>
</html>